//登录的方法


var user;

function login() {
    var account = $("#account").val();
    var password = $("#password").val();
    console.log(account)
    console.log(password)
    $.ajax({
        url: "/login",
        method: "post",
        data: {account: account, password: password},
        success: function (jsonStr) {
            var result = JSON.parse(jsonStr)

            console.log(result)
            if (result.status == 1) {
                console.log("成功");
                user = result;
                window.location.href="/client/index.jsp" ;
            } else {
                showMessage("账号或密码错误");
            }
        }
    })
}

let dataCode;
let account;
let password;

//发送验证码
function sendVerifyCode() {
    var email = $("#email").val();
    var userName = $("#userName").val();
    console.log(userName)
    $.ajax({
        url: "/emailLongin",
        method: "post",
        data: {email: email,userName:userName},
        success: function (jsonStr) {
            var result = JSON.parse(jsonStr);
            console.log(result)
            if(result.status ==1){
                dataCode = result.data
                showMessage("验证码发送成功");
            }else if (result.status ==2){
                showMessage("用户或邮箱不存在!");
            }else if(result.status ==3){
                showMessage("用户和邮箱不匹配！请重新输入")
            }
        }
    })
}

function shhmychh() {
    var email = $("#email").val();
    console.log(email)
    $.ajax({
        url: "/emailLonginCC",
        method: "post",
        data: {email: email},
        success: function (jsonStr) {
            var result = JSON.parse(jsonStr);
            console.log(result)
            if(result.status ==1){
                data = result.data
                account = result.account;
                password = result.password;
                console.log(data)
                console.log(account)
                console.log(password)
                showMessage("验证码发送成功");
            }
        }
    })
}


function updataPassword(){
        var account = $("#account").val();
        var email = $("#email").val();
        var password = $("#password").val();
        var newPassword = $("#newPassword").val();
    console.log(password)
    console.log(newPassword)
        if(password!=newPassword){
            showMessage("两次密码不一致")
        }else if (password==""){
            showMessage("不可为空")
        }else if(password==newPassword){
            $.ajax({
                url:"/UpdataPassword",
                method:"post",
                data:{account:account,email:email,password:password},
                success:function (json){
                    var result = JSON.parse(json);
                    if (result.status ==1){
                        showMessage("修改成功！")
                        window.location.href="/client/login.jsp?account="+result.account+"&password="+password+""
                    }else {
                        showMessage("跳转失败，请联系管理员")
                    }
                }

            })
        }



}

function forgotPassword(){
    var code = $("#code").val();
   var emailI = $("#email").val();
    if (null == code || code.length != 4) {
        showMessage("请输入4位验证码！")
    }
    if(dataCode != code){
        showMessage("验证码错误")
    }else {
        $.ajax({
            url:"/forgotPassword",
            method:"post",
            data:{code:code,email:emailI},
            success:function (json){
                 var result = JSON.parse(json);
                console.log(result)
                 if(result.status==1){
                     showMessage("验证成功");
                     window.location.href="/client/updataByemail.jsp?account="+result.account+"&email="+result.email+""
                 }else {
                     showMessage("跳转页面失败，请联系管理员")
                 }
            }
        })
    }
}

function loginByCode() {
    var code = $("#code").val();
    if (null == code || code.length != 4) {
        showMessage("请输入4位验证码！")
    }
    if(data == code){
        let a = account;
        let p = password;
        $.ajax({
            url:"/login",
            method: "post",
            data:{account:a,password:p},
            success:function (){
                showMessage("登录成功")
                window.location.href="/client/index.jsp" ;
            }
        })
    }
}

// //验证码登录的方法
// function loginByCode() {
//     var phone = $("#phone").val();
//     var code = $("#code").val();
//     if (null == code || code.length != 4) {
//         showMessage("请输入4位验证码！")
//     }
//
//     $.ajax({
//         url: contextPath + "/login",
//         method: "post",
//         data: {phone: phone, code: code, action: "loginByCode"},
//         success: function (jsonStr) {
//             var result = eval("(" + jsonStr + ")");
//             if (result.status == 1) {
//                 window.location.href = contextPath + "/home?action=index";
//             } else {
//                 showMessage(result.message)
//             }
//         }
//     })
// }
